package com.nc.delivery_project.server.dao;

import com.nc.delivery_project.server.domain.Role;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
* Created by miroshnik on 23.12.2014.
*/
public class RoleDao extends AbstractDao<Role> {

    protected final static String ENTITY_NAME = Role.class.getSimpleName();

    protected final static String ROLE_ID = "ROLE_ID";
    protected final static String ROLE_TITLE = "ROLE_TITLE";

    public RoleDao(Connection connection) throws SQLException {
        super(connection, ENTITY_NAME);
    }
    
    public RoleDao() {
        super(ENTITY_NAME);
    }

    @Override
    protected void prepareInsertStatement(CallableStatement statement, Role object) throws SQLException {
        statement.setString(1, object.getTitle());
        statement.registerOutParameter(2, Types.NUMERIC);
        statement.setString(3, object.getTitle());
    }

    @Override
    protected void prepareUpdateStatement(CallableStatement statement, Role object) throws SQLException {
        statement.setString(1, object.getTitle());
        statement.setString(2, object.getTitle());
    }


    @Override
    protected List<Role> parseResultSet(ResultSet rs) throws SQLException {
        List<Role> roles = new ArrayList<>();
        while (rs.next()) {
            int roleId = rs.getInt(ROLE_ID);

            roles.add(new Role(roleId, rs.getString(ROLE_TITLE)));
        }

        return roles;
    }
}
